package com.lishe.lishe.lishe.MyRecyclerView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.lishe.lishe.R;
import com.vivian.timelineitemdecoration.itemdecoration.SpanIndexListener;
import com.vivian.timelineitemdecoration.util.Util;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by shino on 2017/12/1.
 */

public class TimeLineDecoration extends RecyclerView.ItemDecoration {
    public static final int STYLE_DRAW = 0;
    public static final int STYLE_RESOURCE = 1;

    //you can choose to draw or use resource as divider
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STYLE_DRAW, STYLE_RESOURCE})
    public @interface ItemStyle {

    }

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;

    //you can choose the orientation of item decoration
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({VERTICAL, HORIZONTAL})
    public @interface Orientation {

    }

    @TimeLineDecoration.ItemStyle
    private int mStyle = STYLE_DRAW;
    @TimeLineDecoration.Orientation
    private int mOrientation = VERTICAL;
    private Context mContext;

    private int mTopDistance = 0;
    private int mItemInterval = 0;
    private int mItemPaddingLeft = mItemInterval;//default
    private int mItemPaddingRight = mItemInterval;

    Paint mLinePaint;
    private int mLineWidth = 4;
    private int mLineColor = R.color.line_da;

    Drawable mDrawable;
    private int mDotRes = com.vivian.timelineitemdecoration.R.drawable.dot;
    private int imageResurose = 0;

    Paint mDotPaint;
    private int mDotPaddingTop = 0;
    private int mDotRadius = 0;
    private int mDotColor = R.color.line_da;

    String mEnd = "END";
    Paint mTextPaint;
    Rect mTextRect;
    private int mTextColor = Color.WHITE;
    private int mTextSize = 18;

    private int mDotPaddingText = 0;//the distance between dot and text
    private int mBottomDistance = 0;//the distance between dot and the bottom of last child

    //圆的颜色和图片
    private Map<Integer, Integer> circleImage = new HashMap<>();
    private Map<Integer, Integer> circleColor = new HashMap<>();

    private SpanIndexListener mSpanIndexListener;

    public TimeLineDecoration(Context context) {
        mContext = context;

        mTextPaint = new Paint();
        mTextRect = new Rect();
        mLinePaint = new Paint();
        mDotPaint = new Paint();
        mDotPaint.setAntiAlias(true);
        mDotPaint.setDither(true);
    }

    public void setSpanIndexListener(SpanIndexListener spanIndexListener) {
        this.mSpanIndexListener = spanIndexListener;
    }

    //修改item的位置
    int max = 0;

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        Log.e("--getItemOffsets", "getItemOffsets");
        if (mStyle == STYLE_RESOURCE) {
            int i = parent.getChildAdapterPosition(view);
            if (circleImage.get(i) != null) {
                Log.e("i--", i + "!null");
                mDrawable = ContextCompat.getDrawable(mContext, circleImage.get(i));
            } else {
                if (imageResurose != 0) {
                    Log.e("i--", i + "imageResurose");
                    mDrawable = ContextCompat.getDrawable(mContext, imageResurose);
                } else {
                    max = mDotRadius;
                }
            }
        }
        if (max < mDrawable.getIntrinsicWidth()) {
            max = mDrawable.getIntrinsicWidth();
        }
        outRect.left = max;
        outRect.right = 0;
        outRect.bottom = 0;

        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams staggerLayoutParams = (StaggeredGridLayoutManager.LayoutParams) layoutParams;
            int spanIndex = staggerLayoutParams.getSpanIndex();
            if (mSpanIndexListener != null && spanIndex != -1) {
                mSpanIndexListener.onSpanIndexChange(view, spanIndex);
            }
        }
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);

        mLinePaint.setColor(mContext.getResources().getColor(mLineColor));
        mLinePaint.setStrokeWidth(mLineWidth);
        mDotPaint.setColor(mContext.getResources().getColor(mDotColor));
        if (mOrientation == VERTICAL) {
            drawCenterVerticalLine(c, parent);
            drawVerticalCircle(c, parent);
        }
    }

    /**
     * 竖线
     *
     * @param c
     * @param parent
     */
    public void drawCenterVerticalLine(Canvas c, RecyclerView parent) {

        int bottom;
        final int childCount = parent.getChildCount();
        View lastChild = parent.getChildAt(childCount - 1);//取最后一个item
        View child0 = parent.getChildAt(0);
        float x0 = child0.getLeft() / 2;
        float top0 = 0;
        if (childCount > 1) {
            View child = parent.getChildAt(childCount - 2);
            bottom = (child.getTop() > lastChild.getTop() ? child.getTop() : lastChild.getTop());
        } else {
            bottom = lastChild.getTop();
        }
        if (circleImage.get(0) != null) {
            top0 = ContextCompat.getDrawable(mContext, circleImage.get(0)).getIntrinsicHeight() / 2;
        } else if (imageResurose != 0) {
            top0 = mDrawable.getIntrinsicHeight() / 2;
        } else {
            top0 = mDotRadius;
        }
        if (circleImage.get(childCount - 1) == null) {
            top0 = top0 + mDotRadius;
        }
        top0 = top0 + mDotPaddingTop;
        bottom = bottom + mDotPaddingTop;
        c.drawLine(x0, top0, x0, bottom, mLinePaint);
    }

    /**
     * 竖向圆
     *
     * @param c
     * @param parent
     */
    public void drawVerticalCircle(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            int top = child.getTop() + mDotPaddingTop;
            int bottom;
            int drawableLeft;
            if (mStyle == STYLE_RESOURCE) {
                if (circleImage.get(i) != null) {
                    mDrawable = ContextCompat.getDrawable(mContext, circleImage.get(i));
                    bottom = top + mDrawable.getIntrinsicHeight();
                    int cha = (child.getLeft() - mDrawable.getIntrinsicWidth()) / 2;
                    drawableLeft = child.getLeft() - cha;
                    mDrawable.setBounds(cha, top, drawableLeft, bottom);//绘制区域限定
                    mDrawable.draw(c);
                } else {
                    if (imageResurose != 0) {
                        mDrawable = ContextCompat.getDrawable(mContext, imageResurose);
                        bottom = top + mDrawable.getIntrinsicHeight();
                        int cha = (child.getLeft() - mDrawable.getIntrinsicWidth()) / 2;
                        drawableLeft = child.getLeft() - cha;
                        mDrawable.setBounds(cha, top, drawableLeft, bottom);//绘制区域限定
                        mDrawable.draw(c);
                    } else {
                        top = top + mDotRadius;
                        drawableLeft = child.getLeft() / 2;
                        c.drawCircle(drawableLeft, top, mDotRadius, mDotPaint);
                    }
                }
            } else {
                top = top + mDotRadius;
                drawableLeft = child.getLeft() / 2;
                c.drawCircle(drawableLeft, top, mDotRadius, mDotPaint);
            }
        }
    }

    public static class Builder {
        private Context mContext;
        private TimeLineDecoration itemDecoration;

        public Builder(Context context) {
            this.mContext = context;
            itemDecoration = new TimeLineDecoration(context);
        }

        public TimeLineDecoration.Builder setOrientation(@TimeLineDecoration.Orientation int orientation) {
            itemDecoration.mOrientation = orientation;
            return this;
        }

        public TimeLineDecoration.Builder setItemStyle(@TimeLineDecoration.ItemStyle int itemStyle) {
            itemDecoration.mStyle = itemStyle;
            return this;
        }

        public TimeLineDecoration.Builder setTopDistance(float distance) {
            itemDecoration.mTopDistance = Util.Dp2Px(mContext, distance);
            return this;
        }

        public TimeLineDecoration.Builder setItemPaddingLeft(float itemPaddingLeft) {
            itemDecoration.mItemPaddingLeft = Util.Dp2Px(mContext, itemPaddingLeft);
            return this;
        }

        public TimeLineDecoration.Builder setItemPaddingRight(float itemPaddingRight) {
            itemDecoration.mItemPaddingRight = Util.Dp2Px(mContext, itemPaddingRight);
            return this;
        }

        public TimeLineDecoration.Builder setItemInterVal(float interval) {
            itemDecoration.mItemInterval = Util.Dp2Px(mContext, interval);
            return this;
        }

        public TimeLineDecoration.Builder setLineWidth(float lineWidth) {
            itemDecoration.mLineWidth = Util.Dp2Px(mContext, lineWidth);
            return this;
        }

        public TimeLineDecoration.Builder setLineColor(int lineColor) {
            itemDecoration.mLineColor = lineColor;
            return this;
        }

        public TimeLineDecoration.Builder setDotRes(int dotRes) {
            itemDecoration.mDotRes = dotRes;
            return this;
        }

        public TimeLineDecoration.Builder setDotRadius(int dotRadius) {
            itemDecoration.mDotRadius = Util.Dp2Px(mContext, dotRadius);
            return this;
        }

        public TimeLineDecoration.Builder setDotColor(int dotColor) {
            itemDecoration.mDotColor = dotColor;
            return this;
        }

        public TimeLineDecoration.Builder setTextColor(int textColor) {
            itemDecoration.mTextColor = textColor;
            return this;
        }

        public TimeLineDecoration.Builder setTextSize(float textSize) {
            itemDecoration.mTextSize = Util.Sp2Px(mContext, textSize);
            return this;
        }

        public TimeLineDecoration.Builder setDotPaddingText(float dotPaddingText) {
            itemDecoration.mDotPaddingText = Util.Dp2Px(mContext, dotPaddingText);
            return this;
        }

        public TimeLineDecoration.Builder setBottomDistance(float bottomDistance) {
            itemDecoration.mBottomDistance = Util.Dp2Px(mContext, bottomDistance);
            return this;
        }
        public TimeLineDecoration.Builder setDotPaddingTop(float mDotPaddingTop) {
            itemDecoration.mDotPaddingTop = Util.Dp2Px(mContext, mDotPaddingTop);
            return this;
        }

        public TimeLineDecoration.Builder setEndText(String end) {
            itemDecoration.mEnd = end;
            return this;
        }

        public TimeLineDecoration.Builder setImageResourse(int id) {
            itemDecoration.imageResurose = id;
            return this;
        }

        public TimeLineDecoration.Builder setCicleImageResourse(int id, int position) {
            itemDecoration.circleImage.put(position, id);
            return this;
        }

        public TimeLineDecoration.Builder setCicleColor(int id, int position) {
            itemDecoration.circleColor.put(position, id);
            return this;
        }

        public TimeLineDecoration create() {
            return itemDecoration;
        }
    }

}
